package com.tan.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration//标注当前类是配置类
@PropertySource(value={"classpath:db.properties"})
@ImportResource("classpath:tx.xml")
@ComponentScan(basePackages = "com.tan")
public class MyConfig {
    //创建数据源
    @Bean
    public DataSource dataSource(@Value("${mysql.driverClass}") String driverClass,
                                 @Value("${mysql.url}") String url,
                                 @Value("${mysql.user}") String user,
                                 @Value("${mysql.password}") String password){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }
    //创建jdbcTemplate
    @Bean
    public JdbcTemplate jdbcTemplate(@Autowired DataSource dataSource){
        JdbcTemplate jdbcTemplate=new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }
}
